package com.kmzx.one.project.controller;

import com.kmzx.one.common.constants.Constants;
import com.kmzx.one.common.exceptions.NoSuchObjectException;
import com.kmzx.one.project.config.listener.UserRewardListener;
import com.kmzx.one.project.events.UserRewardEvent;
import com.kmzx.one.project.net.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Api(tags = "快美甄选_[ _demo_接口 ]")
@RestController
public class JustDemoWeb extends BaseController {

    @Autowired
    UserRewardListener userRewardListener;

    @Autowired
    private RedisTemplate<String, Object> stringObjectRedisTemplate;

    @ApiOperation(value = "1.测试返回金额", notes = "测试返回金额", position = -1)
    @GetMapping(value = "demo/before")
    public BaseResult before(String userId, String orderId, String orderMoney, Integer profit, Integer inOrOut) {
        try {
            BigDecimal money = new BigDecimal(orderMoney);

            UserRewardEvent event = new UserRewardEvent(userId, money, orderId);
            event.setInOrOut(inOrOut);
            event.setProfit(profit);
            event.setTimeStamp(new Date().getTime());

            userRewardListener.defaultUserRewardEvent(event);


            BaseResult success = BaseResult.Builder.SUCC().initSuccData("OK");
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return handleException(e);
        }


    }

    @ApiOperation(value = "1.测试返回金额", notes = "测试返回金额", position = -1)
    @GetMapping(value = "demo/two")
    public BaseResult before2(String userId) {
        try {
            StringBuilder patchKey = new StringBuilder(Constants.wx_patch_update);
            patchKey.append(userId);

            stringObjectRedisTemplate.opsForValue().set(patchKey.toString(), patchKey);
            Object o = stringObjectRedisTemplate.opsForValue().get(Constants.wx_patch_update + userId);

            String abc = o.toString();
            System.out.println("abc.trim() = " + abc.trim());


            long lastMicroSeconds = Duration.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.MAX)).getSeconds();
            Object s = stringObjectRedisTemplate.opsForValue().get("idCardVerify2:" + userId);
            if (s != null) {
                int num = Integer.parseInt(s.toString());
                if (num > 9) {
                    throw new NoSuchObjectException("今日认证次数已用完,请明日再试");
                }
                num += 1;
                // , lastMicroSeconds, TimeUnit.MICROSECONDS
                stringObjectRedisTemplate.opsForValue().set("idCardVerify2:" + userId, num + "", lastMicroSeconds, TimeUnit.SECONDS);
            } else {
                //, lastMicroSeconds, TimeUnit.MICROSECONDS
                stringObjectRedisTemplate.opsForValue().set("idCardVerify2:" + userId, 1 + "", lastMicroSeconds, TimeUnit.SECONDS);
            }



            BaseResult success = BaseResult.Builder.SUCC().initSuccData("OK");
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return handleException(e);
        }


    }




}
